//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/27.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
        setupUI()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarURL,
            placeholderImage: UIImage(named: "avatar_default_big"))
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(200 - self.view.bounds.height)
        }
        
        UIView.animateWithDuration(1,
            delay: 0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                print("完成")
                NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
        }
    }
    
    //MARK: - 懒加载视图控件
    private lazy var backImageView = UIImageView(imageName: "ad_background")
    private lazy var iconView = UIImageView(imageName: "avatar_default_big")
    private lazy var messageLable = UILabel(title: "欢迎回来", fontSize: 18)
    
}

//MARK: - 设置子控件
extension WelcomeViewController {
    private func setupUI() {
        //添加子控件
        view.addSubview(iconView)
        view.addSubview(messageLable)
        
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        //设置子控件约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.centerX.equalTo(view.snp_centerX)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
}